package com.tianyadu.mmall.controller;

import com.tianyadu.mmall.core.common.ResultResponse;
import com.tianyadu.mmall.entity.SysUser;
import com.tianyadu.mmall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author xueyj
 * @date 2019-06-06 16:07
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(value = "userController", tags = {"用户管理"})
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取授权用户全部信息
     *
     * @param authentication
     * @return java.lang.Object
     * @author xueyj
     * @date 2019/11/4-23:23
     */
    @GetMapping("/me")
    public Object getCurrentUser(Authentication authentication) {
        return authentication;
    }

    /**
     * 获取授权用户，用户信息
     *
     * @param userDetails
     * @return java.lang.Object
     * @author xueyj
     * @date 2019/11/4-23:24
     */
    @GetMapping("/userDetail")
    public Object getUserDetails(@AuthenticationPrincipal UserDetails userDetails) {
        return userDetails;
    }

    /**
     * 获取用户详细信息
     *
     * @param id 用户ID
     * @return com.tianyadu.mmall.user.browser.common.ServerResponse
     * @author xueyj
     * @date 2019/6/6-16:49
     */
    @ApiOperation(value = "获取用户详细信息", notes = "根据用户的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", dataType = "Integer")
    @GetMapping(value = "/findById")
    public ResultResponse findById(@RequestParam(value = "id") int id) {
        log.info("入参信息，id=【{}】", id);
        return ResultResponse.createBySuccess(id);
    }

    /**
     * 获取用户列表
     *
     * @return List
     * @author xueyj
     * @date 2019/6/6-16:48
     */
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @GetMapping(value = "/getUserList")
    public ResultResponse getUserList() {
        return ResultResponse.createBySuccess(new ArrayList<>());
    }

    /**
     * 保存用户
     *
     * @param user 用户信息
     * @return java.lang.String
     * @author xueyj
     * @date 2019/6/6-16:51
     */
    @ApiOperation(value = "保存用户", notes = "保存用户")
    @PostMapping(value = "/saveUser")
    public ResultResponse saveUser(@RequestBody SysUser user) {
        SysUser sysUser = userService.saveUserByJpa(user);
        return ResultResponse.createBySuccess(sysUser);
    }

    /**
     * 修改用户
     *
     * @param id
     * @param username
     * @param password
     * @return java.lang.String
     * @author xueyj
     * @date 2019/6/6-16:52
     */
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "username", value = "用户名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, paramType = "query", dataType = "String")
    })
    @PutMapping(value = "/updateUser")
    public ResultResponse updateUser(@RequestParam(value = "id") int id, @RequestParam(value = "username") String username,
                                     @RequestParam(value = "password") String password) {
        SysUser user = new SysUser();
        return ResultResponse.createBySuccess(user);
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return java.lang.String
     * @author xueyj
     * @date 2019/6/6-16:52
     */
    @ApiOperation(value = "删除用户", notes = "根据用户的id来删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", dataType = "int")
    @DeleteMapping(value = "/deleteUserById")
    public ResultResponse deleteUserById(@RequestParam(value = "id") int id) {
        return ResultResponse.createBySuccess(id);
    }

}